package controllerlayer;

import java.util.ArrayList;

import modellayer.Appointment;
import modellayer.KeyPerson;
import modellayer.ZipCode;
import dblayer.AppointmentDB;

/**
 * The AppointmentCtrl class to handle apppointment information
 * 
 * @author Allan Eriksen, Andreas Rye, Casper Madsen, Christian Stidsborg,
 *         Stefan Bols
 */

public class AppointmentCtrl {
	private AppointmentDB appointmentDB;
	private KeyPersonCtrl kpCtrl;
	private ZipCodeCtrl zcCtrl;

	/**
	 * @param appointmentDB
	 * @param kpCtrl
	 * @param zcCtrl
	 */

	/**
	 * Constructor for the appointmentCtrl
	 */
	public AppointmentCtrl() {
		appointmentDB = new AppointmentDB();
		kpCtrl = new KeyPersonCtrl();
		zcCtrl = new ZipCodeCtrl();
	}

	/**
	 * Method used to create an appointment object and puts further to
	 * appointmentDB which will insert it into the Database. If failure, this
	 * method throws an exception.
	 * 
	 * @param subject
	 * @param fromDate
	 * @param toDate
	 * @param address
	 * @param zip
	 * @param addedDate
	 * @param kp
	 * @return success
	 */
	public boolean createAppointment(String subject, long fromDate,	long toDate, String address, ZipCode zip, long addedDate, KeyPerson kp) {
		boolean success = false;

		Appointment a = new Appointment(-1, subject, fromDate, toDate, address,	zip, addedDate, kp);

		try {
			if (appointmentDB.insertAppointment(a) != -1) {
				success = true;
			}
		} catch (Exception e) {
			System.out
					.println("Failed to insert appointment into database! (AppointmentCtrl) Error: "
							+ e);
		}

		return success;
	}

	/**
	 * Method used to return all objects in the appointment ArrayList since
	 * wClause is null
	 * 
	 * @return appointmentDB.getAppointments()
	 */
	public ArrayList<Appointment> getAllAppointments() {
		return appointmentDB.getAppointments(null);
	}

	 public boolean deleteAppointment(int appointmentId) throws Exception {
		 return appointmentDB.deleteAppointment(appointmentId);
	 }
	 
	 public Appointment findAppointment(int appointmentId) {
		 return appointmentDB.findAppointment(" appointmentId="+appointmentId);
	 }

}
